<?php

	function buildDisplayTree() {
                $CI =& get_instance();
                $CI->load->library('api/services/Menu');
		$options = Menu::menuRootList();
		$html = '<ul>';

		foreach($options as $key => $option) {
			$html .= '<li><a href=\'?menu_id='.$option['menu_id'].'\'>'.$option['name'].'</a>';
			$subMenu = Menu::subMenuList($option['menu_id']);
			if (!empty($subMenu)) 
				$html .= buildSubMenuTree($option['menu_id']);
			$html .= '</li>';
		}
		$html .= '</ul>';

	 return $html;
	}
	
	function buildSubMenuTree($parent_menu_id) {
		$html = '';
		$firstChild = false;
                $subMenu = Menu::subMenuList($parent_menu_id);

		if (!empty($subMenu)) {
			foreach($subMenu as $key => $option) {
				if($firstChild === false) {
					$firstChild = true;
					$html .= '<ul>';
				}
				$html .= '<li><a href=\'?menu_id='.$option['menu_id'].'\'>'.$option['name'].'</a>';

				// rekurzija
                                $subMenu = Menu::subMenuList($option['menu_id']);
				if (!empty($subMenu))
					$html .= buildSubMenuTree($option['menu_id']);
			}

			if ($firstChild === true) {
				$html .= '</ul>';
			}
			$html .= '</li>';
		}

	 return $html;
	}
        
        echo buildDisplayTree();
        
?>
